package com.v3admin.system.controller;

import com.v3admin.common.controller.IResponse;
import com.v3admin.system.domain.SystemUser;
import com.v3admin.system.domain.vo.SystemUserVO;
import com.v3admin.system.service.SystemUserRoleService;
import com.v3admin.system.service.SystemUserService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * 系统用户
 *
 * @author Junpeng.Li
 * @date 2023-12-12 09:09
 */
@Validated
@RestController
@RequestMapping(value = "/system/user")
@AllArgsConstructor
public class SystemUserController {

    private final SystemUserService systemUserService;

    private final SystemUserRoleService systemUserRoleService;

    /**
     * 获取用户信息
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping(value = "/info")
    public IResponse getUserInfo(@NotNull(message = "用户ID不能为空") Long userId) {
        SystemUser user = systemUserService.getById(userId);
        return IResponse.success(SystemUserVO.convert(user));
    }

    /**
     * 管理员添加用户
     *
     * @param user 用户信息
     * @return 操作结果
     */
    @PostMapping
    public IResponse addUser(@RequestBody @Valid SystemUser user) {
        systemUserService.save(user);
        return IResponse.success("用户添加成功");
    }

    /**
     * 给指定的用户设置角色
     *
     * @param userId  用户ID
     * @param roleIds 角色ID集合, 最少一个
     * @return 操作结果
     */
    @PostMapping(value = "/role")
    public IResponse setRole(@NotNull(message = "用户ID不能为空") Long userId,
                             @RequestBody @NotEmpty(message = "角色ID不能为空") Set<Long> roleIds) {
        systemUserRoleService.setRole(userId, roleIds);
        return IResponse.success("角色设置成功");
    }
}
